﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yitter.IdGenerator;

namespace CoreShop.App
{
    static class Program
    {
        [DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")]
        public static extern int GetSystemDefaultLCID();
        [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]
        public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);
        public const int LOCALE_SLONGDATE = 0x20;
        public const int LOCALE_SSHORTDATE = 0x1F;
        public const int LOCALE_STIME = 0x1003;

        public static string mDownLoadPath = Application.StartupPath + "\\DownLoad";

        /// <summary>
        /// 创建一个静态的对象,方便给服务端调用
        /// </summary>
        public static FOrdering frmOrdering { get; set; }


        public static FMainWin frmMainWin { get; set; }

        /// <summary>
        /// 创建一个静态的对象,方便给服务端调用
        /// </summary>
        public static frmCustomerDisplay frmCustShow { get; set; }

        /// <summary>
        /// 创建一个静态的对象,方便给服务端调用
        /// </summary>
        public static fpgDataMonitor frmDataMonitor { get; set; }

        /// <summary>
        /// 创建一个静态的对象,方便给服务端调用
        /// </summary>
        public static frmMainPage frmMainPage { get; set; }



        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Process pc = Process.GetCurrentProcess();
            if (Process.GetProcessesByName(pc.ProcessName).Length > 1)
            {
                UIMessageBox.Show("程序已在运行!", "提示");
                Application.Exit();
                return;
            }

            //Application.Run(new FMainWin());

            // 雪花漂移算法
            // 创建 IdGeneratorOptions 对象，请在构造函数中输入 WorkerId：
            var options = new IdGeneratorOptions(1);
            //options.WorkerIdBitLength = 1;
            // WorkerIdBitLength 默认值6，支持的 WorkerId 最大值为2^6-1，若 WorkerId 超过64，可设置更大的 WorkerIdBitLength
            // options.WorkerIdBitLength = 10; 
            // ...... 其它参数设置参考 IdGeneratorOptions 定义，一般来说，只要再设置 WorkerIdBitLength （决定 WorkerId 的最大值）。

            // 保存参数（必须的操作，否则以上设置都不能生效）：
            YitIdHelper.SetIdGenerator(options);

            SetDateTimeFormat();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if ((Directory.Exists(mDownLoadPath)) && (Directory.GetFiles(mDownLoadPath).Length > 0))
            {
                string msg = "发现新版本，是否马上升级?\r\n 注：推荐升级到最新版本！";
                if (MessageBox.Show(msg, "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    string toolPath = Application.StartupPath + "\\UpgradeWork.exe";
                    if (File.Exists(toolPath))
                    {
                        Process.Start(toolPath);
                        Application.Exit();
                    }
                    else
                        UIMessageBox.Show("未发现调用升级的程序", "提示");
                }
            }


            bool loginFlag = false;
            var frm = new frmLogin();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                loginFlag = true;
            }
            frm.Dispose();
            if (!loginFlag)
            {
                Application.Exit();
                return;                
            }
            frmMainWin = new FMainWin();
            //frmMainPage = new frmMainPage();
            //frmMainPage.Hide();
            Application.Run(frmMainWin);
        }

        public static void SetDateTimeFormat()
        {
            try
            {
                int x = GetSystemDefaultLCID();
                SetLocaleInfo(x, LOCALE_STIME, "HH:mm:ss");        //时间格式  
                SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd");   //短日期格式    
                SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy-MM-dd");   //长日期格式   
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        //前台下载的后台接口
        //var msgDataModel = new MsgDataModel();
        //msgDataModel.DateTime = "2023-07-10 18:05:01";
        //msgDataModel.StoreId = 1005;
        //msgDataModel.UserId = 1132;
        //msgDataModel.ItemId = 28;
        //var ret = ManualDataCache.Instance.Set("corpId_20231005", msgDataModel);
        //var msgDataModel0 = ManualDataCache.Instance.Get<MsgDataModel>("corpId_20231005");
        //var msgDataModel1 = ManualDataCache.Instance.Get<MsgDataModel>("corpId_20231006");

    }
}
